package cn.tedu.charging.device.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate redisTemplate =  new RedisTemplate();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 设置序列化器 默认使用JDK序列化器
        // 修改为String
        redisTemplate.setKeySerializer(RedisSerializer.string());
        // 设置value 的序列化为 json 格式
        redisTemplate.setValueSerializer(RedisSerializer.json());
        // 设置hash 的key序列化器为String
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        // 设置hash 的value序列化器为json
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
